<template>
  <div class="container">
    <div class="app-container">
      <div class="role-operate">
        <!-- 按钮 -->
        <el-button type="primary" size="mini" @click="showDialog=true">添加角色</el-button>
        <!-- 弹层 -->
        <el-dialog width="500px" :visible.sync="showDialog" title="新增角色" @close="close">
          <el-form ref="form" label-width="120px" :model="form" :rules="rules">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="form.name" size="mini" style="width:300px" />
            </el-form-item>
            <el-form-item label="启用" prop="state">
              <el-switch v-model="form.state" size="mini" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="form.description" type="textarea" size="mini" :rows="3" style="width:300px" />
            </el-form-item>
            <el-form-item>
              <el-row type="flex" justify="center">
                <el-col :span="12">
                  <el-button type="primary" size="mini" @click="btncomfig">确定</el-button>
                  <el-button size="mini" @click="close">取消</el-button>
                </el-col>
              </el-row>
            </el-form-item>
          </el-form>

        </el-dialog>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column prop="name" align="center" width="200" label="角色">
            <template #default="{row}">
              <el-input v-if="row.isEdit" v-model="row.rowEdit.name" size="mini" />
              <span v-else>{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="state" align="center" width="200" label="启用">
            <template #default="{row}">
              <el-switch v-if="row.isEdit" v-model="row.rowEdit.state" :active-value="1" :inactive-value="0" />
              <span v-else>
                {{ row.state=== 1 ? "已启用" :row.state===0 ? "未启用" : "无" }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="description" align="center" label="描述">
            <template #default="{row}">
              <el-input v-if="row.isEdit" v-model="row.rowEdit.description" size="mini" type="textarea" />
              <span v-else>{{ row.description }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template #default="{row}">
              <template v-if="row.isEdit">
                <el-button size="mini" type="primary" @click="confirmEdit(row)">确定</el-button>
                <el-button size="mini" @click="row.isEdit=false">取消</el-button>
              </template>
              <template v-else>
                <el-button type="text">分配权限</el-button>
                <el-button type="text" @click="edit(row)">编辑</el-button>
                <!-- 气泡层 -->
                <el-popconfirm title="这是一段内容确定要删除吗？" @onConfirm="confirmDel(row)">
                  <el-button slot="reference" style="margin-left:10px" type="text">删除</el-button>
                </el-popconfirm>
              </template>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" align="middle" justify="end" style="height:60px">
          <el-pagination
            :current-page="pageParams.page"
            :page-size="pageParams.pagesize"
            :total="total"
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
          />

        </el-row>
      </div>
    </div>
  </div>
</template>
<script>
import { getRoleList, getRolePost, updateRole, delectRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      form: {

        name: '', // 角色名称
        description: '', // 角色描述
        state: 0 // 启用
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      showDialog: false,
      // value: true,
      roleList: [],
      pageParams: {
        page: 1,
        pagesize: 5
      },
      total: 0
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async confirmDel(row) {
      await delectRole(row.id)
      this.$message.success('删除角色成功')
      if (this.roleList.length === 1) {
        this.getRoleList()
      }
    },
    async  confirmEdit(row) {
      if (row.rowEdit.name && row.rowEdit.description) {
        await updateRole({ ...row.rowEdit, id: row.id })
        Object.assign(row, {
          ...row.rowEdit,
          isEdit: false
        })
      } else {
        this.$message.warning('角色和描述不能为空')
      }
    },
    edit(row) {
      row.isEdit = true
      row.rowEdit = {
        name: row.name,
        description: row.description,
        state: row.state
      }
    },
    async btncomfig() {
      await this.$refs.form.validate()
      await getRolePost(this.form)
      this.$message.success('新增角色成功')
      this.close()
      this.getRoleList()
    },
    close() {
      this.$refs.form.resetFields()
      this.showDialog = false
    },
    handleCurrentChange(val) {
      this.pageParams.page = val
      this.getRoleList()
    },
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageParams)
      this.total = total
      this.roleList = rows
      this.roleList.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'rowEdit', {
          name: item.name,
          state: item.state,
          description: item.description

        })
      })
      // console.log(total)
    }
  }
}
</script>
<style scoped>
.role-operate{
  padding: 10px;
}
</style>
